<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_transaksi extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_transaksi_model', 'GLTrans');
		$this->load->model('keu_transaksi_detail_model', 'GLTransDetail');
		$this->load->model('combo_model', 'Combo');
		$this->load->model('keu_coa_model', 'Coa');
		
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "GL Transaksi";
		$data['data'] = null;
		$data['data']['date_from'] = date('01/m/Y');
		$data['data']['date_to'] = date('t/m/Y');
		
		viewByTemplate('keu_transaksi/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->GLTrans->GetJSON();
	}
	
	function getdetaildata()
	{
		$this->secure_session();
		echo $this->GLTrans->GetDetailJSON(post('id_dok'));
	}
	
	
	
	function combo_activitydata(){
		$this->secure_session();
		//echo json_encode($this->Activity->GetComboGridData(post('q')));
	}
	
	function combo_projectdata(){
		$this->secure_session();
		echo json_encode($this->GLTrans->GetComboProjectData(post('q')));
	}
	
	function combo_coadata(){
		$this->secure_session();
		echo json_encode($this->Coa->GetComboGridData(post('q')));
	}
	
		
	function create()
	{
		$data['title'] = "Tambah GL Transaksi";
		$data['data'] = NULL;
		$data['office_data']		= json_encode($this->Combo->SWOffice());
		$data['valuta_data']		= json_encode($this->Combo->SWValuta());
		$data['cost_center_data']	= json_encode($this->Combo->SWCostCenter());
		
		$data['mode'] = "create";
		viewByTemplate('keu_transaksi/form', $data, 'form');
	}

	function edit()
	{
		$data['title'] = "Ubah GL Transaksi";
		$data['data'] = $this->GLTrans->GetSingleView(get('id_dok'));
				
		$data['office_data']		= json_encode($this->Combo->SWOffice());
		$data['valuta_data']		= json_encode($this->Combo->SWValuta());
		$data['cost_center_data']		= json_encode($this->Combo->SWCostCenter());
		
		
		$data['mode'] = "edit";
		viewByTemplate('keu_transaksi/form', $data, 'form');
	}

	
	function GetKurs(){
		
		$tgl = post('tgl_transaksi') == '' ? post('tgl_umk')  : post('tgl_transaksi');
		$arrkurs = $this->GLTrans->GetKurs(post('kd_valuta'), date_format3($tgl, 'YYYY-MM-DD'));
		echo $arrkurs['harga_rata'];
	}
	
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		// Validation
		if (post("kd_kantor") == '') {
			$error->Add("kd_kantor", "Kantor", $tpl_empty);
		}
		
		if (post("tgl_transaksi") == '') {
			$error->Add("tgl_transaksi", "Tanggal Transaksi", $tpl_empty);
		}
		/*
		if (post("trans_type_id") == '') {
			$error->Add("trans_type_id", "Tipe Transaksi", $tpl_empty);
		}
		
		if (post("code_account") == '') {
			$error->Add("code_account", "Bank Akun", $tpl_empty);
		}
		*/
		if (post("kd_valuta") == '') {
			$error->Add("kd_valuta", "Mata Uang", $tpl_empty);
		}
		
		if (post("keterangan") == '') {
			$error->Add("keterangan", "Keterangan", $tpl_empty);
		}
		
		return $error;
	}
	
	function dtl_account_data()
	{
		echo $this->GLTransDetail->GetJSON();
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('id_dok', 'kd_kantor', 'tgl_transaksi',  'keterangan',  'kurs', 'kd_valuta'));
				
				
				$data['kurs']			= toNumber($data['kurs']);
				
				
				if (post("mode") == "create")
				{
				
					$suffix 	= "/GL/".date_format3(post('tgl_transaksi'), 'MM').'/'.date_format3(post('tgl_transaksi'), 'YYYY');
					$id_dok		= $this->get_data("SELECT (NVL((SELECT MAX(SUBSTR(ID_DOK,0,LENGTH(ID_DOK)-LENGTH('".$suffix."'))) FROM KEU_TRANSAKSI WHERE SUBSTR(ID_DOK,-1*LENGTH('".$suffix."'),LENGTH('".$suffix."')) = '".$suffix."'),0)+1) CODE FROM DUAL");
					$id_dok		= str_pad($id_dok, 2, '0', STR_PAD_LEFT).$suffix;
					
					$data['id_dok']					= $id_dok;
					$data['kd_tipe_jurnal']			= 'JR000'; //jurnal umum
					$data['status_posting']			= 'N';
					$data['status_verifikasi']		= 'N';
					$data['tgl_transaksi']			= date_format3(post('tgl_transaksi'), 'YYYY-MM-DD');
					$data['create_date']			= NOW;
					$data['create_user']			= USERID;

					$this->GLTrans->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
				
					$data['tgl_transaksi']		= date_format3(post('tgl_transaksi'), 'YYYY-MM-DD');
					$data['modify_date']	= NOW;
					$data['modify_user']	= USERID;
					
					$this->GLTrans->Update($data, 
										array('id_dok'	=> $data['id_dok']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
				
				
				// Detail Payment
				$dtl_account	= $this->data['dtl_account'];
				$total_deleted	= $dtl_account['num_row']['deleted'];
				$total_inserted	= $dtl_account['num_row']['inserted'];
				$total_updated	= $dtl_account['num_row']['updated'];
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_account['deleted'][$i];
					$this->GLTransDetail->DeleteSingle($row['id_dok'], $row['kd_costatus_center'], $row['id_coa']);
				}
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					$row = $dtl_account['inserted'][$i];
					
					
					unset($row['nama_coa']);
					unset($row['nama_cost_center']);
					
					$row['debet']		= toNumber($row['debet'])=='NaN' ? 0 : toNumber($row['debet']);
					$row['kredit']		= toNumber($row['kredit'])=='NaN' ? 0 : toNumber($row['kredit']);
					$row['kurs']		= $data['kurs'];
									
					$row['debet_c']		= $row['kurs']*$row['debet'];
					$row['kredit_c']	= $row['kurs']*$row['kredit'];
					
					$row['id_dok'] = $data['id_dok'];
					
					$row['create_date']	= NOW;
					$row['create_user']	= USERID;
					
					$this->GLTransDetail->Add($row);
				}
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_account['updated'][$i];
					
					unset($row['nama_coa']);
					unset($row['nama_cost_center']);
					
					$row['debet']		= toNumber($row['debet'])=='NaN' ? 0 : toNumber($row['debet']);
					$row['kredit']		= toNumber($row['kredit'])=='NaN' ? 0 : toNumber($row['kredit']);
					$row['kurs']		= $data['kurs'];
									
					$row['debet_c']		= $row['kurs']*$row['debet'];
					$row['kredit_c']	= $row['kurs']*$row['kredit'];
					
					$row['id_dok'] = $data['id_dok'];
					
					$row['modify_date']	= NOW;
					$row['modify_user']	= USERID;
				
				
					$this->GLTransDetail->Update($row, array(	'id_dok'	=> $row['id_dok'], 
																'kd_costatus_center'	=> $row['kd_costatus_center'], 
																'id_coa'		=>  $row['id_coa'] ));
				}
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$id_dok	= get('id_dok');
		
		if ($id_dok != "")
		{
			// Delete Single
			$ls_result = $this->GLTrans->DeleteSingle($id_dok);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->GLTrans->DeleteSingle($row['id_dok']);
			}
		}
		
		echo $ls_result;
	}
	
	
	
}
?>